package com.longcai.piaojia.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/7/16.
 */
public class DateTimePickDialogBefore implements DatePicker.OnDateChangedListener {

    private Context context;

    private DatePicker datePicker;
    //    private TimePicker timePicker;
    private AlertDialog ad;
    private String dateTime;
    private String initDateTime;


    private TextView endDate;
    private String mEndTime;
    private String mStartTime;
    private ChooseDate chooseDate;

    public DateTimePickDialogBefore(Context context) {
        this.context = context;
    }

    public DateTimePickDialogBefore(Context context, TextView endDate, String startTime, String endTime) {

        this.context = context;
        this.endDate = endDate;
        this.mEndTime = endTime;
        this.mStartTime = startTime;

        chooseDate = (ChooseDate) context;
    }

    public AlertDialog dateTimePicKDialog(final TextView inputDate) {

        Calendar calendar = Calendar.getInstance();

        initDateTime = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH);

        LinearLayout dateTimeLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.common_datetime, null);

        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);

        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), this);

        ad = new AlertDialog.Builder(context)
                .setTitle(initDateTime)
                .setView(dateTimeLayout)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        inputDate.setText(dateTime);

                        chooseDate.chooseDone(dateTime);

                        //这是投保页面的 设置值
                        String[] arr = dateTime.split("-");

                        if (endDate != null) {
                            int year = Integer.parseInt(arr[0]);

                            String time = String.valueOf(year) + "-" + arr[1] + "-" + arr[2];

                            endDate.setText(time);
                        }

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
//                        inputDate.setText("");
                    }
                }).show();

        onDateChanged(datePicker, 0, 0, 0);
        return ad;
    }

    //    控制时间不能选择今天之前的时间
    private boolean isDateBefore(DatePicker tempView) {

        Calendar mStartCalendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {

            mStartCalendar.setTime(sdf.parse(mStartTime));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar tempCalendar = Calendar.getInstance();

        tempCalendar.set(tempView.getYear(), tempView.getMonth(), tempView.getDayOfMonth(), 0, 0, 0);

        if (tempCalendar.before(mStartCalendar))
            return true;
        else
            return false;
    }

    private boolean isDateAfter(DatePicker tempView) {

        Calendar mEndCalendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {

            mEndCalendar.setTime(sdf.parse(mEndTime));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar tempCalendar = Calendar.getInstance();

        tempCalendar.set(tempView.getYear(), tempView.getMonth(), tempView.getDayOfMonth(), 0, 0, 0);

        if (tempCalendar.after(mEndCalendar))
            return true;
        else
            return false;
    }


    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // 获得日历实例
        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //这个控制  日期  不超过当天
        if (isDateBefore(view)) {
            Calendar mCalendar = Calendar.getInstance();

            try {

                mCalendar.setTime(sdf.parse(mStartTime));

            } catch (ParseException e) {
                e.printStackTrace();
            }

            view.init(mCalendar.get(Calendar.YEAR),
                    mCalendar.get(Calendar.MONTH),
                    mCalendar.get(Calendar.DAY_OF_MONTH), this);
        }

        if (isDateAfter(view)) {
            Calendar mCalendar = Calendar.getInstance();

            try {

                mCalendar.setTime(sdf.parse(mEndTime));

            } catch (ParseException e) {
                e.printStackTrace();
            }

            view.init(mCalendar.get(Calendar.YEAR),
                    mCalendar.get(Calendar.MONTH),
                    mCalendar.get(Calendar.DAY_OF_MONTH), this);
        }

        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth());

        dateTime = sdf.format(calendar.getTime());

        ad.setTitle(dateTime);
    }

    public interface ChooseDate {

        void chooseDone (String dateTime);
    }

}
